package com.pengtu.web.project;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.project.Relationship;
import com.pengtu.service.project.ProjectManager;
import com.pengtu.service.project.RelationshipManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

public class RelationshipAction extends BaseActionSupport<Relationship> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private RelationshipManager relationshipManager;
	private ProjectManager projectManager;

	private String id;
	private Relationship relationship;
	private List<Relationship> relationshipList = new ArrayList<Relationship>();
	private ProjectInfo info;
	private List<ProjectInfo> projList = new ArrayList<ProjectInfo>();

	@Override
	public Relationship getModel() {
		// TODO Auto-generated method stub
		return relationship;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	
	//根据工程名称模糊查询项目单位表
	public List<Relationship> findDimQuery() {

		// 获得工程输入字符
		String projName = Struts2Utils.getParameter("q");

		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 依据工程名查找包含输入字符 的工程
		queryFilter.addFilter("proj.projName", projName);


		relationshipList = relationshipManager.findByListRelationship(queryFilter);
		String htmlStr = "";
		if (relationshipList.size() > 0) {
			htmlStr = "<h3>查询到" + relationshipList.size() + "条工程名称为: 【" + projName
					+ "】 的单位信息！</h3><ol>";
			for (int i = 0; i < relationshipList.size(); i++) {
				Relationship rela = relationshipList.get(i);
				String  showValue =  rela.getProj().getProjName() +"_" +  rela.getEntrustorgid();
				htmlStr += "<li><a href = \" #\" onclick=\" setValue('"
						+ showValue + "','"
						+ rela.getId()
						+ "'); \"> <font colour = \"yellow\"> "
						+ showValue + "</font></a></li>";
			}
			htmlStr += "<li><a href=\"#\" onclick=\"addOrg();\">【新增】</a>;</li>";
		} else {
			htmlStr = "<h3>没有查询到工程名称为'【" + projName + "】'的工程信息，建议您：</h3><ol>";
			htmlStr += "<li>1 : 修改关键字,重新查询;</li>";
			htmlStr += "<li>2 : 选择 <a href=\"#\" onclick=\"addOrg();\">【新增】</a>，添加工程信息;</li>";
		}
		Struts2Utils.renderText(htmlStr += "</li></ol>");

		return null;
	}
	
	//根据Id查找并使用Json传输
	public  String findByRelation(){
		String relationId = Struts2Utils.getParameter("ralationId");
		StringBuffer strBuffer = new StringBuffer("({ success:");
		if(StringUtils.isNotEmpty(relationId)){
			Relationship relationship = relationshipManager.findByPropertyName("id", relationId);
			if(null!=relationship){
				strBuffer.append("true,relationship:{");
				if(StringUtils.isNotEmpty(relationship.getEntrustorgid())){
						strBuffer.append("entrustorgid:\""+relationship.getEntrustorgid()+"\"," );	
				}
				if(StringUtils.isNotEmpty(relationship.getWitnessorgid())){
					strBuffer.append("witnessorgid:\""+relationship.getWitnessorgid()+"\"," );	
				}
				if(StringUtils.isNotEmpty(relationship.getWitnesses())){
					strBuffer.append("witnesses:\""+relationship.getWitnesses()+"\"," );	
				}
					
				if(StringUtils.isNotEmpty(relationship.getWphone())){
					strBuffer.append("wphone:\""+relationship.getWphone()+"\"," );	
				}
				if(StringUtils.isNotEmpty(relationship.getOperations())){
					strBuffer.append("operations:\""+relationship.getOperations()+"\"," );	
				}
				if(StringUtils.isNotEmpty(relationship.getOphone())){
					strBuffer.append("ophone:\""+relationship.getOphone()+"\"," );	
				}
				if(StringUtils.isNotEmpty(relationship.getCheckpeople())){
					strBuffer.append("checkpeople:\""+relationship.getCheckpeople()+"\"," );	
				}
				if(StringUtils.isNotEmpty(relationship.getCheckphone())){
					strBuffer.append("checkphone:\""+relationship.getCheckphone());	
				}
				strBuffer.append("\"}})");
				System.out.println(strBuffer.toString());
				Struts2Utils.renderJson(strBuffer.toString());
			}
		}
		return NONE;
	}
	
	/**
	 * 检查车牌号是否已注册过
	 * @return
	 */
	public void findByRelationProjName() {
		String projId = Struts2Utils.getParameter("projectId");
		String entrustOrgId = Struts2Utils.getParameter("entrustorgid");
		QueryFilter queryFilter = new QueryFilter();
		List<Relationship> relations = new ArrayList<Relationship>();
		queryFilter.addFilter("proj.id",projId);
		queryFilter.addFilter("entrustorgid",entrustOrgId);
		
		relations = relationshipManager.findByListRelationship(queryFilter);
		if(relations.size()==0){
			Struts2Utils.renderText("true");
		}else{
			Struts2Utils.renderText("false");
		}
	}


	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		projList = projectManager.getPageProj();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		
		relationship.setProj(projectManager.getProj("id", info.getId()));
		relationshipManager.saveRelationship(relationship);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
			addActionMessage(mes+"项目单位成功");
			id = relationship.getId();
			return input();
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			relationship = relationshipManager.findByPropertyName("id", id);
		} else {
			relationship = new Relationship();
		}
	}

	public ProjectInfo getInfo() {
		return info;
	}

	public void setInfo(ProjectInfo info) {
		this.info = info;
	}

	public List<Relationship> getRelationshipList() {
		return relationshipList;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<ProjectInfo> getProjList() {
		return projList;
	}

	public void setRelationshipList(List<Relationship> relationshipList) {
		this.relationshipList = relationshipList;
	}

	@Autowired
	public void setRelationshipManager(RelationshipManager relationshipManager) {
		this.relationshipManager = relationshipManager;
	}

	@Autowired
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

}
